#include <windows.h>
#include"resource.h"
#include<stdio.h>
#include<set>
#include<malloc.h>
#include<string.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>

struct node{
        int inf;
};

struct el_lista_noduri{
        node N;
        el_lista_noduri *next;
        el_lista_noduri *child[8];
};

el_lista_noduri *head=NULL;
el_lista_noduri *root=NULL;

HWND d1=NULL;

char *buffer=(char*)malloc(1000*sizeof(char));
char *buffer1=(char*)malloc(1000*sizeof(char));

void insert(int val)
{
        int i;

        el_lista_noduri *nou=NULL,*p=NULL;
        nou=(el_lista_noduri*)malloc(sizeof(el_lista_noduri)*1000);
        nou->N.inf=val;
        if(head==NULL)
        {
                for(i=0;i<8;i++)
                        nou->child[i]=NULL;
                nou->next=NULL;
                head=nou;
        }
        else
                if(nou->N.inf<head->N.inf)
                {
                        for(i=0;i<8;i++)
                                nou->child[i]=NULL;
                        nou->next=head;
                        head=nou;
                }
                else
                {
                        p=head;
                        while((p->next!=NULL)&&(p->next->N.inf<nou->N.inf))
                                p=p->next;
                        for(i=0;i<8;i++)
                                nou->child[i]=NULL;
                        nou->next=p->next;
                        p->next=nou;
                }
                root=head;
                for(i=0;i<8;i++)
                        root->child[i]=NULL;
}
void creare_arbore(el_lista_noduri *&root)
{
        el_lista_noduri *p=(el_lista_noduri*)malloc(sizeof(el_lista_noduri)*1000);
        int i;
        if(head==NULL)
        {
                //MessageBox(d1,"nu exista niciun element de inserat!","nu stiu",MB_OK | MB_ICONINFORMATION);
                return;
        }
        p=head->next;
        while(p!=NULL)
        {
                int contor=0;
                for(i=0;i<8;i++)
                        if(p!=NULL && contor!=8)
                        {
                                root->child[i]=p;
                                root->child[i]->N.inf=p->N.inf;
                                for(int j=0;j<8;j++)
                                        root->child[i]->child[j]=NULL;
                                p=p->next; 
                                contor++;
                        }
                if(contor==8)
                {
                        for(int k=0;k<8;k++)
                                for(int j=0;j<8;j++)
                                        if(p!=NULL)
                                        {
                                                root->child[k]->child[j]=p;
                                                root->child[k]->child[j]->N.inf=p->N.inf;
                                                for(int y=0;y<8;y++)
                                                        root->child[k]->child[j]->child[y]=NULL;
                                                p=p->next;
                                        }
                }
                
        }
        
        
}
void cautare(int val)
{
        el_lista_noduri *p=NULL;
        p=head;
        char *buf1=(char*)malloc(10000000*sizeof(char));
        int ok=0,j=0;
        int i=0;
        int m;
        int nr=0;
        creare_arbore(p);
        if(p==NULL)
        {
                MessageBox(d1,"nu exista elemente in arbore","cautare",MB_OK | MB_ICONINFORMATION);
                return;
        }
        if(val<p->N.inf)
        {
                MessageBox(d1,"elementul nu exista in arbore:","cautare",MB_OK | MB_ICONINFORMATION);
                return;
        }
        if(p->N.inf==val)
        {
                MessageBox(d1,"valoarea a fost gasita si se afla pe primul nivel al alborelui este radacina arborelui:","cautare",MB_OK | MB_ICONINFORMATION);
                return;
        }
        for(i=0;i<8;i++)
		    {
                   if(p->child[i]!=NULL)
                       {
                                if(p->child[i]->N.inf==val)
                                {
                                        nr++;
                                        ok=1;
                                        m=i;
                                }
                        }
                        
              }
                if(ok==0)
                        MessageBox(d1,"numarul nu exista in arbore:","cautare",MB_OK | MB_ICONINFORMATION);
                else
                {
                        sprintf(buf1,"numarul a fost gasit pe nivelul 1,pozitia:%d si a fost gasit de %d ori:",m+1,nr);
                        MessageBox(d1,buf1,"cautare",MB_OK | MB_ICONINFORMATION);
                        return;
                }
        
               i=0;
                while(i<8)
                { 
                        int v=0;
                        if(p->child[i]->child[0]!=NULL && p->child[i]->child[7]!=NULL)
                        {
                                if(p->child[i]->child[0]->N.inf<=val && val<=p->child[i]->child[7]->N.inf)
                                {
                                        for(j=0;j<8;j++)
                                                if(p->child[i]->child[j]!=NULL)
                                                {
                                                        if(p->child[i]->child[j]->N.inf==val)
                                                        {
                                                                nr++;
                                                                ok=1;
                                                                m=j;
                                                        }
                                                }
                                        if(ok==1)
                                        {
                                                sprintf(buf1,"numarul a fost gasit de %d pe nivelul doi ca fiind al %d copil",nr,m+1);
                                        MessageBox(d1,buf1,"cautare",MB_OK | MB_ICONINFORMATION);
                                                return;
                                        }
                                }
                                else
                                        i++;
                }
                else
                {
                        while(v<8)
                                {
                                        if(p->child[i]->child[v]!=NULL)
                                                if(p->child[i]->child[v]->N.inf==val)
                                                {
                                                        nr++;
                                                        ok=1;
                                                        m=v;
                                                }
                                                v++;
                                }
                        if(ok==1)
                                        {
                                                sprintf(buf1,"numarul a fost gasit de %d pe nivelul doi ca fiind al %d copil",nr,m+1);
                                                MessageBox(d1,buf1,"cautare",MB_OK | MB_ICONINFORMATION);
                                                return;
                                        }
                        if(ok==0)
                                {
                                        MessageBox(d1,"numarul nu a fost gasit in arbore:","cautare",MB_OK | MB_ICONINFORMATION);
                                        return;
                                }
                }

        }

}
/*void stergere(el_lista_noduri *nod,int val)
{
        el_lista_noduri *ajutor,*q;
        nod=head;
        int ok=0;
        if(nod==NULL)
        {
                printf("arborele nu are niciun element");
                return;
        }
        if(nod->N.inf==val)
        {
                ajutor=nod;
                nod=nod->next;
                free(ajutor);
                ok=1;
        }
        else
        {
                q=nod;
                while(q->next!=NULL && q->next->N.inf!=val)
                        q=q->next;
                if(q->N.inf==val)
                {
                        ajutor=q;
                        q=q->next;
                        free(ajutor);
                        ok=2;
                }
        }
        if(ok==1)
                {
                        creare_arbore(nod);
                        return;
                }
        else
                if(ok==2)
                {
                        creare_arbore(q);
                        return;
                }
                else

                {
                        printf("nu a fost gasit elementul pe care dorim sa l stergem,deci arborele ramane neschimbat!");
                        return;
                }
}*/
/*void stergere(int v[],int n,int val)
{
        int nr=0,i;
        for(i=0;i<n;i++)
                if(v[i]==val)
                {
                        while(i<n-1)
                        {
                                v[i]=v[i+1];
                                i++;
                        }
                        nr++;
                }
                if(nr!=0)
					 MessageBox(hwnd,"S-a sters valoarea data din arbore","Stergere",MB_OK | MB_ICONINFORMATION);
				else
					MessageBox(hwnd,"Valoarea data nu exista arbore","Stergere",MB_OK | MB_ICONINFORMATION);
}*/
void resetare()
{
        el_lista_noduri *p;
        while(head!=NULL)
        {
                p=head;
                head=head->next;
                free(p);
        }
}
void stergere(el_lista_noduri *head,int val)
{
	el_lista_noduri *p=(el_lista_noduri*)malloc(sizeof(el_lista_noduri)*1000000);
	el_lista_noduri *q=(el_lista_noduri*)malloc(sizeof(el_lista_noduri)*1000000);
	int ok=0;
	if(head==NULL)
	{
		MessageBox(d1,"arborul nu are elemente","Stergere",MB_OK | MB_ICONINFORMATION);
		return;
	}
	if(head->N.inf==val)
	{
		q=head;
		head=head->next;
		free(q);
		ok=1;
	}
	q=head;
	while(q->next!=NULL && q->next->N.inf!=val)
		q=q->next;
	if(q->next!=NULL && q->next->N.inf==val)
	{
		p=q->next;
		q->next=q->next->next;
		free(p);
		ok=1;
	}

	
	if(ok==1)
		MessageBox(d1,"S-a sters valoarea data din arbore","Stergere",MB_OK | MB_ICONINFORMATION);
	else
		MessageBox(d1,"Valoarea data nu se afla in  arbore","Stergere",MB_OK | MB_ICONINFORMATION);

}


int transformare(char a[100])
{
        int b;
        b=atoi(a);
        printf("%d",b);
        return b;
}



const char g_szClassName[] = "myWindowClass";
// functia de creare unei casete de dialog
BOOL CALLBACK dialog1 (HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{

       switch (msg)
        {
        case WM_INITDIALOG:
                return TRUE;
                break;
        case WM_COMMAND:
                switch (LOWORD(wParam))
                {
                                case ID_INSERT:
                                        int c;
                                        GetDlgItemText(hwnd,IDC_EDIT1,buffer,5);
                                        c=transformare(buffer);
                                        insert(c);
                                       // creare_arbore(root);
                                        MessageBox(hwnd,"S-a inserat cu succes","Insert",MB_OK | MB_ICONINFORMATION);
                        break;
                                case IDC_cautare:
                                        int d;
                                        GetDlgItemText(hwnd,IDC_EDIT1,buffer1,5);
                                        d=transformare(buffer1);
                                        cautare(d);
                                        break;
                                case IDC_resetare:
                                        int k;
                                        resetare();
                                        MessageBox(hwnd,"Arborele a fost resetat","Resetare",MB_OK | MB_ICONINFORMATION);
                                        break;
								case IDC_stergere:
										int m;
										GetDlgItemText(hwnd,IDC_EDIT1,buffer,5);
										m=transformare(buffer);
										stergere(head,m);
										creare_arbore(root);
										break;
                                case IDC_OK:
										EndDialog(hwnd,0);
										MessageBox(hwnd,"S-a iesit cu succes","Exit",MB_OK | MB_ICONINFORMATION);
								break;
               
                }
                break;
                case WM_CLOSE:
                EndDialog(hwnd,0);
                break;
        default:
                return FALSE;
        }
        return TRUE;
}
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
        HWND d1;
    switch(msg)
    {
        case WM_CLOSE:
            DestroyWindow(hwnd);
        break;
        case WM_DESTROY:
            PostQuitMessage(0);
        break;
                case WM_COMMAND:
                        switch(LOWORD(wParam))
                        {
                        case ID_FILE_NEW40001:
                        d1=CreateDialog(GetModuleHandle(NULL),MAKEINTRESOURCE(IDD_DIALOG1),hwnd,dialog1);
                        if (d1!=NULL)
                        {
                                ShowWindow(d1,SW_SHOW);
                        }
                        else
                        {
                                MessageBox(hwnd,"Eroare la crearea dialogului 1","EROARE",MB_OK | MB_ICONERROR);
                        }
                        break;
                        case ID_AJUTOR_DESPRE:
                                 MessageBox(hwnd,"Acest proiect a fost facut de catre Atanasescu Robert Anton si Dunareanu Sorin","Ajutor",MB_OK | MB_ICONERROR);
                                 break;
                        case ID_FILE_EXIT:
                                exit(1);
								break;
                        }
                
                default:
            return DefWindowProc(hwnd, msg, wParam, lParam);
    }
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    LPSTR lpCmdLine, int nCmdShow)
{
    WNDCLASSEX wc;
    HWND hwnd;
    MSG Msg;

    //Step 1: Registering the Window Class
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;
    wc.cbWndExtra    = 0;
    wc.hInstance     = hInstance;
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+10);
    wc.lpszMenuName  = MAKEINTRESOURCE(IDR_MENU1);
    wc.lpszClassName = g_szClassName;
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);

    if(!RegisterClassEx(&wc))
    {
        MessageBox(NULL, "Window Registration Failed!", "Error!",MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    // Step 2: Creating the Window
    hwnd = CreateWindowEx(
        WS_EX_CLIENTEDGE,
        g_szClassName,
        "arbore",
        WS_OVERLAPPEDWINDOW,
        CW_USEDEFAULT, CW_USEDEFAULT, 800, 600,
        NULL, NULL, hInstance, NULL);

    if(hwnd == NULL)
    {
        MessageBox(NULL, "Window Creation Failed!", "Error!",
            MB_ICONEXCLAMATION | MB_OK);
        return 0;
    }

    ShowWindow(hwnd, nCmdShow);
    UpdateWindow(hwnd);

    // Step 3: The Message Loop
    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
        TranslateMessage(&Msg);
        DispatchMessage(&Msg);
    }
    return Msg.wParam;
}
